package com.woniuxy.microretailserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microretailserver.bean.*;
import com.woniuxy.microretailserver.mapper.GoodsOrderMapper;
import com.woniuxy.microretailserver.mapper.GoodsPayMapper;
import com.woniuxy.microretailserver.mapper.OrderGoodsMapper;
import com.woniuxy.microretailserver.service.IGoodsOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenhu
 * 新增购买订单业务层接口实现类
 */
@Transactional(readOnly = true)
@Service
public class GoodsOrderServiceImpl implements IGoodsOrderService {

    @Resource
    private GoodsOrderMapper goodsOrderMapper;
    @Resource
    private OrderGoodsMapper orderGoodsMapper;
    @Resource
    private GoodsPayMapper goodsPayMapper;

    /**
     * 新增订单及商品信息业务层代码
     *
     * @param goodsOrderBean
     * @param orderGoodsBean
     * @Param goodsPayBean
     * @return
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int saveGoodsOrder(GoodsOrderBean goodsOrderBean, List<OrderGoodsBean> orderGoodsBean, GoodsPayBean goodsPayBean) {
        goodsOrderMapper.saveGoodsOrder(goodsOrderBean);
        orderGoodsBean.forEach(e->{
            e.setGoodsOrderBean(goodsOrderBean);
            orderGoodsMapper.saveOrderGoods(e);
        });
        goodsPayMapper.saveGoodsPay(goodsPayBean);
        return 0;
    }

    /**
     * 分页查询商品购买订单
     * @param request
     * @param goodsOrderBean
     * @return
     */
    @Override
    public PageInfo<GoodsOrderBean> findAllGoodsOrder(PageRequest request, GoodsOrderBean goodsOrderBean) {
        return PageHelper.startPage(request.getPageNum(),request.getLimit()).
                doSelectPageInfo(()->goodsOrderMapper.findAllGoodsOrder(request, goodsOrderBean));
    }

    /**
     * 根据订单号查询详细信息
     * @param orderNo
     * @return
     */
    @Override
    public HashMap<String, Object> findAll(String orderNo) {
        GoodsOrderBean byOrderNo = goodsOrderMapper.findByOrderNo(orderNo);
        GoodsPayBean byIdPay = goodsPayMapper.findByIdPay(byOrderNo.getId());
        List<OrderGoodsBean> byIdOrder = orderGoodsMapper.findByIdOrder(byOrderNo.getId());
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("orderNo",byOrderNo);//订单信息
        stringObjectHashMap.put("pay",byIdPay);//支付信息
        stringObjectHashMap.put("order",byIdOrder);//商品信息
        return stringObjectHashMap;
    }

    /**
     * 分页查询订单及支付信息
     * @param request
     * @param goodsOrderBean
     * @return
     */
    @Override
    public PageInfo<GoodsOrderBean> findAllGoodsOrderPay(PageRequest request, GoodsOrderBean goodsOrderBean) {
        return PageHelper.startPage(request.getPageNum(),request.getLimit()).
                doSelectPageInfo(()->goodsOrderMapper.findAllGoodsOrderPay(request, goodsOrderBean));
    }

    /**
     * 导出订单数据
     * @return
     */
    @Override
    public List<GoodsOrderExportBean> findByAllOrder() {
        return goodsOrderMapper.findByAll();
    }

    /**
     * 导出订单与账户表数据
     * @return
     */
    @Override
    public List<GoodsOrderPayExportBean> findByAllGoodsPay() {
        return goodsOrderMapper.findByAllGoodsPay();
    }
}
































